package com.example.teamtribe.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.widget.Toast;

import com.example.teamtribe.Application;
import com.example.teamtribe.database.User;

import org.litepal.crud.DataSupport;

import java.io.File;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.List;

public class Utility {

    private static Toast toast;

    @Retention(RetentionPolicy.SOURCE)
    @IntDef({Toast.LENGTH_SHORT, Toast.LENGTH_LONG})
    private @interface NavigationMode {}

    public static void makeToast(String content, @NavigationMode int l) {
        if (toast == null) {
            toast = Toast.makeText(Application.getContext(), content, l);
        } else {
            toast.setText(content);
        }
        toast.show();
    }

    public synchronized static void setToken(String token) {
        SharedPreferences.Editor editor = Application.getContext().getSharedPreferences("isLogged", Context.MODE_PRIVATE).edit();
        editor.putString("token", token);
        editor.apply();
    }

    @Nullable
    public static String getToken() {
        SharedPreferences pref = Application.getContext().getSharedPreferences("isLogged", Context.MODE_PRIVATE);
        return pref.getString("token", "");
    }

    public synchronized static void setUser(@NonNull User user) {
        SharedPreferences.Editor editor = Application.getContext().getSharedPreferences("isLogged", Context.MODE_PRIVATE).edit();
        editor.putInt("realId", user.getRealId());
        editor.apply();
        user.saveOrUpdate();
    }

    @NonNull
    public static User getUser() {
        SharedPreferences pref = Application.getContext().getSharedPreferences("isLogged", Context.MODE_PRIVATE);
        int realId = pref.getInt("realId", 1);
        List<User> users = DataSupport.where("realId = ?", realId + "").find(User.class);
        return users.get(0);
    }

    @Nullable
    public static <T> T getT(int realId, Class<T> modelClass) {
        List<T> data = DataSupport.where("realId = ?", realId + "").limit(1).find(modelClass);
        if (data == null || data.isEmpty()) {
            return null;
        }
        return data.get(0);
    }

    public static void setLogged(boolean isLogged) {
        SharedPreferences.Editor editor = Application.getContext().getSharedPreferences("isLogged", Context.MODE_PRIVATE).edit();
        editor.putBoolean("isLogged", isLogged);
        editor.apply();
    }

    public static boolean isLogged() {
        SharedPreferences pref = Application.getContext().getSharedPreferences("isLogged", Context.MODE_PRIVATE);
        boolean is = pref.getBoolean("isLogged", false);
        if (is) {
            int realId = pref.getInt("realId", 1);
            //LitePal.use(LitePalDB.fromDefault("TeamTribe" + realId));
        }
        return is;
    }

    public static Uri getUri(String fileName) {
        return Uri.fromFile(new File(Application.getContext().getFilesDir() + File.separator + fileName));
    }

    public static void setFirst() {
        SharedPreferences.Editor editor = Application.getContext().getSharedPreferences("isLogged", Context.MODE_PRIVATE).edit();
        editor.putBoolean("first", false);
        editor.apply();
    }

    public static boolean isFirst() {
        SharedPreferences pref = Application.getContext().getSharedPreferences("isLogged", Context.MODE_PRIVATE);
        return pref.getBoolean("first", true);
    }
}
